#importing system files...
import sys
import os
import shutil
import string

#importing local files....
import dir_fxns

def copy_files(path_binaries,platform):
    dirs=dir_fxns.listdir_check(path_binaries)
    if dirs==-1001:
        print "\nError occured while listing the contents of directory of \
path specified.... \nPlease check it...."
        print "Program exiting...."
        exit(1)
        
    #list_dirs=[]  #This list is to store and check whether "xx.xx"
                  #version type directory already present or not...
    
    #Here the vPath-dissector directory is made after the list
    #command, so it will not be there in the dirs list...
    vpath_binaries_path=path_binaries+"/vPath-dissector"
    retcode=dir_fxns.mkdir_p(vpath_binaries_path)
    if retcode!=0:
        print "-----------------------------------------------------------\
---------------"
        print "Error!!!\nUnable to create %s directory!!!" \
% vpath_binaries_path   #If unable to create directory when it doesn't
                        #exist, then display the error message....
        print "Exiting from the program. First correct the error and then \
re-execute the program......"
        print "------------------------------------------------------------\
--------------"
        exit(1)
                
    for dir in dirs:
        path=path_binaries+"/"+dir+"/lib/wireshark/plugins"
        lib_dirs=dir_fxns.listdir_check(path)
        if lib_dirs==-1001:
            print "--------------------------------------------------------\
------------------"
            print "\nNo plugins directory found in %s directory."\
 % (path_binaries+"/"+dir)
            print "Program tried to look for path %s" % path
            print "It couldn't find it. So skipping it and moving further...."
            print "--------------------------------------------------------\
------------------"
            continue
        if len(lib_dirs)!=0:
            for lib_dir in lib_dirs:
                #name_parts=lib_dir.split('.')
                #ver=name_parts[0]+"."+name_parts[1]
                #similar_version_present=0
                #for version in list_dirs:
                #    if ver==version:
                #        #Create symbolic link to it.
                #        get_list_ver=dir_fxns.listdir_check(path_binaries+\
                #"/vPath-dissector")
                #        for dir_ver in get_list_ver:
                #            dir_ver_parts=dir_ver.split('.')
                #            dir_ver_comp=dir_ver_parts[0]+"."+dir_ver_parts[1]
                #            if ver==dir_ver_parts:
                #                os.chdir(path_binaries+"/vPath-dissector")
                #                destn="../"+dir_ver
                #                link="../"+lib_dir
                #                xx=subprocess.Popen(["ln",
                #"-s",destn,link],shell=False).wait()
                #                break
                #        similar_version_present=1
                #        break
                #if similar_version_present==1:
                #    continue
                #else:
                #    list_dirs.append(ver)
                lib_path=vpath_binaries_path+"/"+lib_dir
                retcode=dir_fxns.mkdir_p(lib_path)
                if retcode!=0:
                    print "----------------------------------------------\
----------------------------"
                    print "Error!!!\nUnable to create %s directory!!!"\
 % lib_path         #If unable to create directory when it doesn't
                    #exist,then display the error message....
                    print "Exiting from the program. First correct the error\
 and then re-execute the program......"
                    print "-----------------------------------------------\
---------------------------"
                    exit(1)
                
                if string.upper(platform)=="X86":
                    platform_path=lib_path+"/x86"
                else:
                    platform_path=lib_path+"/x86_64"
                retcode=dir_fxns.mkdir_p(platform_path)
                if retcode!=0:
                    print "-----------------------------------------------\
---------------------------"
                    print "Error!!!\nUnable to create %s directory!!!"\
 % platform_path    #If unable to create directory when it doesn't
                    #exist,then display the error message....
                    print "Exiting from the program. First correct the error\
 and then re-execute the program......"
                    print "------------------------------------------------\
 --------------------------"
                    exit(1)
                    
                path_source=path+"/"+lib_dir
                shutil.copy(path_source+"/vpath.so",platform_path)
                shutil.copy(path_source+"/vpath.la",platform_path)
    return 0

if __name__=="__main__":
    path_binaries=sys.argv[1]
    platform=sys.argv[2]
    x=copy_files(path_binaries,platform)
    print "Done...."
